// pages/search/index.js
import {goifOpen,goCollection,readliang} from '../../utils/util'
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    key:'',
    list:[],
    num:1,
    hasMoreData:true,
    pageSize:20
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const vm = this;
    vm.setData({key:options.key})
    vm.getList()
  },
  getList(callback){
    const vm = this;
    if (vm.data.key){
      wx.getStorage({
        key:'userInfo',
        success:function(e){
          wx.request({
            url: 'https://www.bchzh.xin/hzmobilework/app/fileClassification/getFilePage',
            // url: 'http://192.168.0.30:8080/hzmobilework/app/fileClassification/getFilePage',
            data: { userId:e.data.id,keyword: vm.data.key, pageNumber: vm.data.num, pageSize: vm.data.pageSize},
            header: { 'content-type': 'application/json' },
            success(res) {
              // console.log(res.data.data.list);
              vm.setData({ list: vm.data.list.concat(res.data.data.page.list.map(function(e){
                if(e.keycontent){
                  e.keycontent = e.keycontent.replace(new RegExp(vm.data.key,"g"),'<span style="color:red;">'+vm.data.key+'</span>')
                  // console.log(e);

                  WxParse.wxParse('cont','html',e.keycontent,vm)
                  e.nodes = vm.data.cont.nodes;
                }
                return e;
              }))})
              if (res.data.data.page.list.length == vm.data.pageSize){
                // vm.setData({ num: vm.data.num++})
                vm.data.num++
              }else{
                vm.setData({hasMoreData:false})
              }
              callback && callback()
            }
          })
        }
      })
    }else{
      wx.showToast({title:'关键字为空',icon:'none'})
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const vm = this;
    vm.setData({ num: 1})
    vm.setData({ list: [] })

    vm.getList(()=>{
      wx.stopPullDownRefresh()
    })
    // console.log(this);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const vm = this;
    if (vm.data.hasMoreData){
      vm.getList()
    }else{
      wx.showToast({ title: '没有更多数据', icon:'none'})
    }
    
  },
  
  godetails(e){
    var data = e.currentTarget.dataset.da;
    // console.log(data)
    // return;
    //type   0视频  1  文档   2  目录
    if(data.type == '2'){
      readliang(data.id,()=>{
        wx.navigateTo({ url: '/pages/class/index?className=' + data.title + '&id=' + data.id })
      })
    }else if((data.type == '1')||(data.type == '0')){
      readliang(data.id,()=>{
        goifOpen(data)
      })
    }
  },
  collection(e){
    var data = e.currentTarget.dataset.da;
    const vm = this;
    if(data.iscollection!="1"){
      goCollection(data.id,()=>{
        vm.data.list.forEach(element => {
          if(element.id == data.id){
            element.iscollection = '1'
          }  
        })
        
        vm.setData({
          list:vm.data.list
        })
      });
    }
  },
  
})